-- Read the weapon_real_base if you really want to know what each action does

if (SERVER) then
	AddCSLuaFile("shared.lua")
	SWEP.HoldType = "ar2"
end

if (CLIENT) then
	SWEP.PrintName 		= "COLT M4A1"
	SWEP.ViewModelFOV		= 60
	SWEP.Slot 			= 3
	SWEP.SlotPos 		= 1
	SWEP.IconLetter 		= "w"

	killicon.AddFont("weapon_real_cs_m4a1", "CSKillIcons", SWEP.IconLetter, Color( 255, 80, 0, 255 ))
end

SWEP.Instructions 		= "Damage: 20% \nRecoil: 5% \nPrecision: 80% \nRate of Fire: 780 rounds per minute"

SWEP.Base 				= "weapon_real_base_special_aim"

SWEP.Spawnable 			= true
SWEP.AdminSpawnable 		= true

SWEP.ViewModel 			= "models/weapons/v_rif_m4a1.mdl"
SWEP.WorldModel 			= "models/weapons/w_rif_m4a1.mdl"

SWEP.Primary.Sound 		= Sound("Weapon_m4a1.Single")
SWEP.Primary.Damage 		= 20
SWEP.Primary.Recoil 		= 0.5
SWEP.Primary.NumShots 		= 1
SWEP.Primary.Cone 		= 0.02
SWEP.Primary.ClipSize 		= 30
SWEP.Primary.Delay 		= 0.08
SWEP.Primary.DefaultClip 	= 30
SWEP.Primary.Automatic 		= true
SWEP.Primary.Ammo 		= "smg1"

SWEP.IronSightsPos 		= Vector (3.963, -3.271, 1.7058)
SWEP.IronSightsAng 		= Vector (1.5025, 0.6891, 0)

SWEP.data 				= {}
SWEP.mode 				= "auto"
SWEP.data.newclip 		= false

SWEP.data.zoomfov 		= 80
SWEP.data.snipefov 		= 0

SWEP.data.semi 			= {}

SWEP.data.auto 			= {}

/*---------------------------------------------------------
Initialize
---------------------------------------------------------*/
function SWEP:Initialize()
	self.data.sync = true
	self.data.init = true
	
	if (SERVER) then
		self:SetWeaponHoldType(self.HoldType)
	end
end

/*---------------------------------------------------------
Auto/Semi Configuration
---------------------------------------------------------*/
function SWEP.data.semi.Init(self)
	self.Primary.Automatic = false
	self.Weapon:EmitSound("weapons/smg1/switch_single.wav")
end
function SWEP.data.auto.Init(self)
	self.Primary.Automatic = true
	self.Weapon:EmitSound("weapons/smg1/switch_burst.wav")
end

/*---------------------------------------------------------
Think
---------------------------------------------------------*/
function SWEP:Think()
	if self.data.init then		
		self.Weapon:SetClip1( self.Weapon:Clip1() - 1 )
		self.data.init = nil
	end
		if self.data.newclip then
		if self.data.newclip == 0 then
			self.data.newclip = false
			
			if self:Ammo1() > self.Primary.ClipSize - 1 then
				if self.data.oldclip == 0 then
					self.Weapon:SetClip1( self.Weapon:Clip1() - 1 )
					if SERVER then
						self.Owner:GiveAmmo(1,self.Primary.Ammo,true)
					end
				end
			end
		else
			self.data.newclip = self.data.newclip - 1
		end
	end
	if self.deployed then
		if self.deployed == 0 then
			if self.data.silenced then
				self.Weapon:SendWeaponAnim(ACT_VM_IDLE_SILENCED)
			else
				self.Weapon:SendWeaponAnim(ACT_VM_IDLE)
			end
			self.deployed = false
			self.Weapon:SetNextPrimaryFire( CurTime() + .001 )
		else
			self.deployed = self.deployed - 1
		end
	end

 if !self.Owner:KeyDown(IN_USE) then 

 if self.Owner:KeyPressed(IN_ATTACK2) then
		self:SetIronsights(true)
		self.Owner:SetFOV( 70, 0.15 )
		self.Owner:SetNetworkedInt("ScopeLevel", 1)
	if CLIENT then return end
 	end
end

 if self.Owner:KeyReleased(IN_ATTACK2) then
		self:SetIronsights(false)
		self.Owner:SetFOV( 0, 0.15 )
		self.Owner:SetNetworkedInt("ScopeLevel", 0)
	if CLIENT then return end
	end
end

/*---------------------------------------------------------
PrimaryAttack
---------------------------------------------------------*/
function SWEP:PrimaryAttack()

	if !self.Owner:KeyDown(IN_USE) then
		if ( !self:CanPrimaryAttack() ) or self.data.newclip or self.data.init then
			if self.data.silenced then
				self.Weapon:SendWeaponAnim(ACT_VM_DRYFIRE_SILENCED)
			else
				self.Weapon:SendWeaponAnim(ACT_VM_DRYFIRE)
			end
			return
		end

	if not self:CanPrimaryAttack() or self.Owner:WaterLevel() > 1 then return end
	-- If your gun have a problem or if you are under water, you'll not be able to fire

	self.Reloadaftershoot = CurTime() + self.Primary.Delay
	-- Set the reload after shoot to be not able to reload when firering

	self.Weapon:SetNextSecondaryFire(CurTime() + self.Primary.Delay)
	-- Set next secondary fire after your fire delay

	self.Weapon:SetNextPrimaryFire(CurTime() + self.Primary.Delay)
	-- Set next primary fire after your fire delay

	self:TakePrimaryAmmo(1)
	-- Take 1 ammo in you clip

	self.Weapon:EmitSound(self.Primary.Sound)
	-- Emit the gun sound when you fire
	
	if (self:GetIronsights() == true) then
		self:CSShootBullet(self.Primary.Damage, 0, self.Primary.NumShots, self.Primary.Cone)
		-- Put 0 recoil when you're in ironsight mod

		self.Owner:ViewPunch(Angle(math.Rand(-0.5,-2.5) * (self.Primary.Recoil / 4), math.Rand(-1,1) * (self.Primary.Recoil / 4), 0))
		-- Punch the screen 2x less hard when you're in ironsigh mod
	else
		self:CSShootBullet(self.Primary.Damage, self.Primary.Recoil, self.Primary.NumShots, self.Primary.Cone)
		-- Put normal recoil when you're not in ironsight mod

		self.Owner:ViewPunch(Angle(math.Rand(-0.5,-2.5) * self.Primary.Recoil, math.Rand(-1,1) *self.Primary.Recoil, 0))
		-- Punch the screen
	end

	else
		if self.data.silenced then
			self.Weapon:SendWeaponAnim(ACT_VM_DETACH_SILENCER)
			self.Primary.Sound			= Sound("Weapon_M4A1.Single")
			self.data.silenced = false
		else
			self.Weapon:SendWeaponAnim(ACT_VM_ATTACH_SILENCER)
			self.Primary.Sound			= Sound("Weapon_M4A1.Silenced")
 			self.data.silenced = true
		end
		self.Weapon:SetNextPrimaryFire( CurTime() + 2 )
	end
end


/*---------------------------------------------------------
SecondaryAttack
---------------------------------------------------------*/
function SWEP:SecondaryAttack()
	if self.Owner:KeyDown(IN_USE) then
		if self.mode == "semi" then
			self.mode = "auto"
		else
			self.mode = "semi"
		end
		self.data[self.mode].Init(self)
		
		if self.mode == "auto" then
			self.Weapon:SetNetworkedInt("csef",1)
		elseif self.mode == "semi" then
			self.Weapon:SetNetworkedInt("csef",3)
		end

	elseif SERVER then
	end
end

/*---------------------------------------------------------
Reload
---------------------------------------------------------*/
function SWEP:Reload()
	if not ValidEntity(self.Owner) then return end
	if (SERVER) then
		self.Owner:SetFOV(0, 0.3)
	end

	if self.data.silenced then
		self.Weapon:DefaultReload(ACT_VM_RELOAD_SILENCED)
	else
		self.Weapon:DefaultReload(ACT_VM_RELOAD)
	end

	self.Owner:SetNetworkedInt("ScopeLevel", 0)
	self:SetIronsights(false)

	return true
end

/*---------------------------------------------------------
Deploy
---------------------------------------------------------*/
function SWEP:Deploy()
	if self.data.silenced then
		self.Weapon:SendWeaponAnim(ACT_VM_DRAW_SILENCED)
	else
		self.Weapon:SendWeaponAnim(ACT_VM_DRAW)
	end
	self.Owner:SetNetworkedInt("ScopeLevel", 0)
	self.Weapon:SetNextPrimaryFire(CurTime() + 1)
	return true
end

/*---------------------------------------------------------
ShootEffects
---------------------------------------------------------*/
function SWEP:CSShootBullet(dmg, recoil, numbul, cone)
	numbul = numbul or 1
	cone = cone or 0.01

	local PlayerAim = self.Owner:GetAimVector()
	local PlayerPos = self.Owner:GetShootPos()

	local bullet = {}
	bullet.Num  = numbul
	bullet.Src = self.Owner:GetShootPos()       -- Source
	bullet.Dir = self.Owner:GetAimVector()      -- Dir of bullet
	bullet.Spread = Vector(cone, cone, 0)     -- Aim Cone
	bullet.Tracer = 1       -- Show a tracer on every x bullets
	bullet.Force = 5        -- Amount of force to give to phys objects
	bullet.Damage = dmg

	self.Owner:FireBullets(bullet)
	if self.data.silenced then
		self.Weapon:SendWeaponAnim(ACT_VM_PRIMARYATTACK_SILENCED)
		self.MuzzleEffect			= "rg_muzzle_silenced"
	else
		self.Weapon:SendWeaponAnim(ACT_VM_PRIMARYATTACK)
		self.Owner:MuzzleFlash()
		self.MuzzleEffect			= "rg_muzzle_hmg"
	end
	self.Owner:SetAnimation( PLAYER_ATTACK1 )

	local fx = EffectData()
	fx:SetEntity(self.Weapon)
	fx:SetOrigin(PlayerPos)
	fx:SetNormal(PlayerAim)
	fx:SetAttachment(self.MuzzleAttachment)
	util.Effect(self.MuzzleEffect,fx)						-- Additional muzzle effects
	
	local fx = EffectData()
	fx:SetEntity(self.Weapon)
	fx:SetNormal(PlayerAim)
	fx:SetAttachment(self.ShellEjectAttachment)
	util.Effect(self.ShellEffect,fx)						-- Shell ejection

	if ((SinglePlayer() and SERVER) or (not SinglePlayer() and CLIENT)) then
		local eyeang = self.Owner:EyeAngles()
		eyeang.pitch = eyeang.pitch - recoil
		self.Owner:SetEyeAngles(eyeang)
	end
end